package com.kehao.leetcode.middle.backtracking_algorithm;

import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LetterCombinationsSolution {
    public List<String> letterCombinations(String digits) {
        HashMap<Character,char[]> map = new HashMap();
        List<String> result = new ArrayList();
        List<char[]> charsList = new ArrayList<>();
        if ("".equals(digits))return result;
        map.put('2',new char[]{'a','b','c'});
        map.put('3',new char[]{'d','e','f'});
        map.put('4',new char[]{'g','h','i'});
        map.put('5',new char[]{'j','k','l'});
        map.put('6',new char[]{'m','n','o'});
        map.put('7',new char[]{'p','q','r','s'});
        map.put('8',new char[]{'t','u','v'});
        map.put('9',new char[]{'w','x','y','z'});
        char[] chars = digits.toCharArray();
        char[] temp = new char[digits.length()];
        for (char c :chars){
            charsList.add(map.get(c));
        }
        fillResult(0,charsList,temp,result);
        return result;
    }

    private void fillResult(int depth, List<char[]> charsList, char[] temp, List<String> result) {
        if(depth==charsList.size()){
            result.add(new String(temp));
        }else {
            char[] chars = charsList.get(depth);
            for (char c :chars){
                temp[depth] = c;
                fillResult(depth+1,charsList,temp,result);
            }
        }
    }

    @Test
    public void test01(){
        System.out.println(letterCombinations("23"));
    }

    @Test
    public void test02(){
        System.out.println(letterCombinations(""));
    }
}
